package com.example.temp.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.temp.entity.AssetInformation;
import com.example.temp.mapper.AssetInformationMapper;
import com.example.temp.utils.RandomNumberUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

@Service
public class AssetInformationService extends ServiceImpl<AssetInformationMapper, AssetInformation> {

    @Autowired
    private AssetInformationMapper assetInformationMapper;

    /**
     * 保存添加的资产信息
     * @param assetInformation
     * @return
     */
    public boolean saveAssetInformation(AssetInformation assetInformation) {
        //添加资产信息
        assetInformation.setAssetNumber(generateUniqueSerialNumber());
        return saveOrUpdate(assetInformation);
    }

    /**
     * 生成生成资产编号
     * @return
     */
    public String generateUniqueSerialNumber() {
        String assetNum;
        boolean isUnique = false;

        Pattern pattern = Pattern.compile("\\d{14}");
        Matcher matcher;

        do {
            // 生成一个14位的随机数
            assetNum = RandomNumberUtil.generateRandomNumber();
            // 匹配正则表达式
            matcher = pattern.matcher(assetNum);
            // 如果数据库中没有相同的资产编号，流水号是唯一的
            if (matcher.matches() && QuerryAssetNumber(assetNum)==null) {
                isUnique = true;
            }
        } while (!isUnique);

        return assetNum;
    }

    /**
     * 检查数据库是否存在相同的资产编号
     * @param assetNumber
     * @return
     */
    public AssetInformation QuerryAssetNumber(String assetNumber){
        QueryWrapper<AssetInformation> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("asset_number", assetNumber);
        return assetInformationMapper.selectOne(queryWrapper);
    }


    /**
     * 用于更新资产状态
     * @param assetNumber
     * @param state
     * @return
     */
    public boolean UpdateAssetState(String assetNumber,String state){
        QueryWrapper<AssetInformation> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("asset_number", assetNumber);
        AssetInformation assetInformation = assetInformationMapper.selectOne(queryWrapper);
        assetInformation.setAssetState(state);
        return saveOrUpdate(assetInformation);
    }


    /**
     * 根据资产编号查找指定资产信息
     * @param assetNum
     * @return
     */
    public AssetInformation selectAssetNum(String assetNum){
         QueryWrapper<AssetInformation>queryWrapper =new QueryWrapper<>();
         queryWrapper.eq("asset_number",assetNum);
         return assetInformationMapper.selectOne(queryWrapper);
     }

     public double AssetMoney(){
         QueryWrapper<AssetInformation>queryWrapper =new QueryWrapper<>();
         List<AssetInformation> list=assetInformationMapper.selectList(queryWrapper);
         Double tmp = 0.0;
         for (AssetInformation asset:list) {
             String price = asset.getPrice();

             if (price != null) {
                 tmp += Double.valueOf(price);
             }
         }
         return tmp;

     }

     public Long AssetCount(){
         QueryWrapper<AssetInformation>queryWrapper =new QueryWrapper<>();
         return assetInformationMapper.selectCount(queryWrapper);

     }

}
